package com.aletto.general_store.ui.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.AuctionGoods;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.ui.activities.ShelvesHitGoodsActivity;
import com.aletto.general_store.ui.popup.AddChainNodePopup;
import com.aletto.general_store.ui.popup.EditAuctionGoodsSwitchPopup;
import com.aletto.general_store.utils.ToastUtils;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.ToastView;

import java.util.List;

public class AuctionGoodsAdapter extends BaseQuickAdapter<AuctionGoods, BaseViewHolder> {

    public AuctionGoodsAdapter() {
        super(R.layout.item_auction_goods);

        addChildClickViewIds(R.id.tv_store_customer, R.id.text3, R.id.text2, R.id.tv_store_delivery);

        setOnItemChildClickListener((adapter, view, position) -> {
            Context context = getContext();
            AuctionGoods item = (AuctionGoods) adapter.getItem(position);

            if (view.getId() == R.id.tv_store_customer) {
                state(position).execute();

            } else if (view.getId() == R.id.text3) {
                new XPopup.Builder(view.getContext())
                        .asCustom(new EditAuctionGoodsSwitchPopup(view.getContext(), item))
                        .show();

            } else if (view.getId() == R.id.text2) {
                // 复制
                if (item.getStatus() == 2) {
                    XHelper.copy2Clipboard(String.valueOf(item.getId()));
                    ToastUtils.makeText(R.string.copy_success);

                } else {
                    delete(position).execute();
                }

            } else if (view.getId() == R.id.tv_store_delivery){
                context.startActivity(ShelvesHitGoodsActivity.newIntent(context, item));
            }

        });
    }

    private SimpleTemplate<Object> state(int position) {
        return new SimpleTemplate<>() {

            @Override
            public INetTask.View<Object> view() {
                return new ToastView<>(getContext(), new INetTask.View<>() {
                    @Override
                    public void success(Object data) {
                        removeAt(position);
                    }
                });
            }

            @Override
            public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
                AuctionGoods item = getItem(position);
                return new NetSingleTask<>(DataRepository.getInstance().upGoodsStatus(item.getId(), item.getStatus() == 2 ? 3 : 2));
            }
        };
    }

    private SimpleTemplate<Object> delete(int position) {
        return new SimpleTemplate<>() {

            @Override
            public INetTask.View<Object> view() {
                return new ToastView<>(getContext(), new INetTask.View<>() {
                    @Override
                    public void success(Object data) {
                        removeAt(position);
                    }
                });
            }

            @Override
            public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
                AuctionGoods item = getItem(position);
                return new NetSingleTask<>(DataRepository.getInstance().delGoods(item.getId()));
            }
        };
    }


    @Override
    protected void convert(@NonNull BaseViewHolder holder, AuctionGoods goods) {
        Resources resources = getContext().getResources();

        holder.setText(R.id.tv_goods_title, goods.getName())
                .setText(R.id.text1, String.format(resources.getString(R.string.sales_and_total_stock), goods.getSales(), goods.getTotal_stock()))
                .setText(R.id.tv_goods_price, goods.getPrice() + " USDT")
                .setText(R.id.text2, goods.getStatus() == 2 ? R.string.copy_link : R.string.delete)
                .setText(R.id.tv_store_customer, goods.getStatus() == 2 ? R.string.remove_from_shelves : R.string.add_from_shelves);

        List<String> goodsImg = goods.getGoods_img();
        if (goodsImg != null && !goodsImg.isEmpty()) {
            ImageView ivGoodsCover = holder.getView(R.id.iv_goods_cover);
            GlideApp.with(ivGoodsCover).load(goodsImg.get(0)).goodsGrid().into(ivGoodsCover);
        }
    }

}
